from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, declarative_base, scoped_session
from configs.setting import Settings
conf=Settings()
DB_URL = f'mysql+pymysql://{conf.db_user}:{conf.db_pass}@{conf.db_host}:{conf.db_port}/{conf.db_name}'
engine = create_engine(DB_URL)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)

Base = declarative_base()
session_factory = scoped_session(SessionLocal)
Base.metadata.create_all(bind=engine)


def get_db():
    db = session_factory()
    try:
        yield db
    finally:
        db.close()
        import pymysql